package com.tanhua.admin.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.admin.mapper.FreezeMapper;
import com.tanhua.admin.mapper.UnfreezeMapper;
import com.tanhua.commons.Constants;
import com.tanhua.dubbo.api.db.UserInfoApi;
import com.tanhua.model.admin.Freeze;
import com.tanhua.model.admin.UnFreeze;
import com.tanhua.model.db.UserInfo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.UserInfoVo;
import com.tanhua.model.vo.UserInfoVoAge;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class ManageServie {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @DubboReference
    private UserInfoApi userInfoApi;

    @Autowired
    private FreezeMapper freezMapper;

    @Autowired
    private UnfreezeMapper unfreezeMapper;
    /**
     *  用户数据翻页
     * @param page
     * @param pagesize
     * @return
     */
    public PageResult getUsers(Long page, Long pagesize) {
        Page<UserInfo> userInfopage = userInfoApi.getUsers(page,pagesize);
        List<UserInfo> userInfos = userInfopage.getRecords();
        List<UserInfoVoAge> voList = new ArrayList<>();
        for (UserInfo userInfo : userInfos) {
            String key = Constants.FREEZE_USER + userInfo.getId();
            UserInfoVoAge init = UserInfoVoAge.init(userInfo);
            if (stringRedisTemplate.hasKey(key)) {
                init.setUserStatus("2");
            }
            voList.add(init);
        }
        return new PageResult(userInfopage.getTotal(),pagesize,userInfopage.getPages(),page,voList);
    }
    /**
     *  用户基本资料
     * @param userID
     * @return
     */
    public UserInfoVo getUser(Long userID) {
        UserInfo getuserinfo = userInfoApi.getuserinfo(userID, null);
        UserInfoVo init = UserInfoVo.init(getuserinfo);
        String key = Constants.FREEZE_USER + getuserinfo.getId();
        if (stringRedisTemplate.hasKey(key)) {
            init.setUserStatus("2");
        }
        return init;
    }
    /**
     * 用户冻结操作
     * @param paramMap
     * @return
     */
    public Map<String, String> freezeUser(Map<String, Object> paramMap) {
        long timeMillis = System.currentTimeMillis();
        Integer userId = (Integer) paramMap.get("userId");
        String freezingTime = (String) paramMap.get("freezingTime");
        String freezingRange = (String) paramMap.get("freezingRange");
        String reasonsForFreezing = (String) paramMap.get("reasonsForFreezing");
        String frozenRemarks = (String) paramMap.get("frozenRemarks");
        String key = Constants.FREEZE_USER + userId;
        String jsonString = JSON.toJSONString(paramMap);
        switch (freezingTime) {
            case "1":
                stringRedisTemplate.opsForValue().set(key,jsonString, 3,TimeUnit.DAYS);break;
            case "2" :
                stringRedisTemplate.opsForValue().set(key, jsonString, 7, TimeUnit.DAYS);break;
            case "3" :
                stringRedisTemplate.opsForValue().set(key, jsonString);break;
        }
        Freeze freez = new Freeze();
        freez.setUserId(userId.longValue());
        freez.setReasonsForFreezing(reasonsForFreezing);
        freez.setFreezingRange(Long.valueOf(freezingRange));
        freez.setFrozenRemarks(frozenRemarks);
        freez.setFreezingTime(Long.valueOf(freezingTime));
        freez.setCreated(timeMillis);
        freez.setUpdated(timeMillis);
        freezMapper.insert(freez);
        Map<String,String>map = new HashMap<>();
        map.put("message","冻结成功");
        return map;
    }
    /**
     *    解除冻结
     * @param paramMap
     * @return
     */
    public Map<String, String> unFreeze(Map<String, Object> paramMap) {
        long timeMillis = System.currentTimeMillis();
        Integer userId = (Integer) paramMap.get("userId");
        String key = Constants.FREEZE_USER + userId;
        String freeze = stringRedisTemplate.opsForValue().get(key);
        JSONObject jsonObject = JSON.parseObject(freeze);
        String reasonsForThawing = (String) paramMap.get("reasonsForThawing");
        UnFreeze unFreeze = new UnFreeze();
        unFreeze.setUserId(userId.longValue());
        unFreeze.setReasonsForFreezing(jsonObject.getString("reasonsForFreezing"));
        unFreeze.setFreezingRange(jsonObject.getInteger("freezingRange").longValue());
        unFreeze.setFrozenRemarks(jsonObject.getString("frozenRemarks"));
        unFreeze.setReasonsForThawing(reasonsForThawing);
        unFreeze.setCreated(timeMillis);
        unFreeze.setUpdated(timeMillis);
        unFreeze.setFreezingTime(jsonObject.getInteger("freezingTime").longValue());
        unfreezeMapper.insert(unFreeze);
        stringRedisTemplate.delete(key);
        Map<String, String> map = new HashMap<>();
        map.put("message","解冻成功");
        return map;
    }
}
